
import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
#配置中文显示

# plt.rcParams['font.family'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False



def draw_pic(path):
#     path就是评价指标.txt的路径
    allValLoss=[]
    allValMiou=[]
    with open(path,'a+') as f:    #�����ļ�����
        f.seek(0.0)#��ָ���Ƶ���ͷ
        while True:#��ȡ���е�����
            text = f.readline()  # ֻ��ȡһ������
            if not text:
                break

            texts=text.split(":")
            if texts[0].lower()=="loss":
                allValLoss.append(float(texts[1]))#��loss�ӵ�Loss�б��С�
            if texts[0].lower()=="miou":
                allValMiou.append(float(texts[1]))#��loss�ӵ�Loss�б��С�




    X=range(len(allValMiou))
    #Y=allValLoss
    plt.figure(figsize=(12,5))

    plt.subplot(1,2,1)
    plt.xlabel("epoch")
    plt.ylabel("loss")
    plt.title("min value={},epoch is {}/{}".format(min(allValLoss),allValLoss.index(min(allValLoss))+1,len(allValLoss)))
    plt.plot(X,allValLoss)
    for x,y in zip(X,allValLoss):
        plt.text(x,y,y)

    plt.subplot(1,2,2)
    plt.xlabel("epoch")
    plt.ylabel("miou")
    plt.title("max value={},epoch is {}/{}".format(max(allValMiou),allValMiou.index(max(allValMiou))+1,len(allValMiou)))
    plt.plot(X,allValMiou)
    for x,y in zip(X,allValMiou):
        plt.text(x,y,y)

    plt.suptitle("Training Result")


    # 保存结果图片
    result = path[:path.rfind('/')]
    plt.savefig(result + '/trainResult.png')




if __name__ == '__main__':
    draw_pic("/home/jolly/develop/project/java/TrainingManager/src/main/resources/trainResult/44/predictResult/评价指标.txt")
    # parser = argparse.ArgumentParser(description='Run training script with given parameters.')
    # parser.add_argument('path', type=int, help='trainMissionId.')
    # args = parser.parse_args()
    #
    # draw_pic(args.path)